<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>11-完整的生命周期</title>
    <script src="lib/react/react.development.js"></script>
    <script src="lib/react/react-dom.development.js"></script>
    <script src="lib/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      class HelloWorld extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            number: 1,
          };
          this.ulRef = React.createRef();
          console.log("1. constructor...");
        }
        static getDerivedStateFromProps(props, state) {
          console.log("2. getDerivedStateFromProps...", this);
          return {
            age: 10,
          };
        }
        componentDidMount() {
          console.log("4. componentDidMount...");
        }
        componentDidUpdate(prevProps, prevState, snapshot) {
          console.log("5. componentDidUpdate...", snapshot);
        }
        shouldComponentUpdate() {
          console.log("7. shouldComponentUpdate...", this.state.number);
          // 判断是否渲染UI
          // return (this.state.number%2==0)
          return true;
        }
        getSnapshotBeforeUpdate(prevProps, prevState) {
          console.log("6. getSnapshotBeforeUpdate...");
          return null;
        }
        incNumber() {
          this.setState((state) => ({
            number: ++state.number,
            age: ++state.age,
          }));
        }
        render() {
          console.log("3. render...", this);
          return (
            <div>
              <ul ref={this.ulRef} onClick={() => this.incNumber()}>
                {Array(this.state.number)
                  .fill(null)
                  .map((item, index) => (
                    <li key={index}>{index}__</li>
                  ))}
              </ul>
              <p>age: {this.state.age}</p>
            </div>
          );
        }
      }
      ReactDOM.render(<HelloWorld />, document.getElementById("root"));
    </script>
  </body>
</html>
